import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

// 定义一个常量作为 localStorage 的键名
const LOCAL_STORAGE_KEY = "foodGroups";

export default new Vuex.Store({
  state: {
    // 全局存储的食物列表分组
    foodGroups: [],
    selectedGroup: "默认组", // 当前选择的组别
  },
  mutations: {
    // 初始化数据（从 localStorage 加载）
    initializeState(state) {
      const storedData = localStorage.getItem(LOCAL_STORAGE_KEY);
      if (storedData) {
        const parsedData = JSON.parse(storedData);
        state.foodGroups = parsedData.foodGroups || [];
        state.selectedGroup = parsedData.selectedGroup || "默认组";
      } else {
        // 如果没有数据，初始化默认值
        state.foodGroups = [
          { 
            name: "默认组", 
            foods: [
              "铁板烧", "火锅", "烧烤", "麻辣烫", "炒饭", "面条", "饺子", "包子", "馄饨", "煎饼果子", 
              "卤肉饭", "红烧肉", "宫保鸡丁", "鱼香肉丝", "糖醋排骨", "清蒸鱼", "水煮鱼", "酸菜鱼", 
              "烤鸭", "炸酱面", "凉皮", "凉面", "锅贴", "春卷", "豆腐脑", "皮蛋瘦肉粥", "叉烧包", 
              "烧麦", "糯米鸡", "肠粉", "牛肉面", "羊肉泡馍", "黄焖鸡米饭", "煲仔饭", "炒年糕", 
              "手抓饼", "油条", "豆浆", "咸鸭蛋", "茶叶蛋", "烤红薯", "炸酥肉", "粉蒸肉", "扣肉"
            ] 
          },
          { 
            name: "快餐", 
            foods: [
              "披萨", "汉堡", "炸鸡", "薯条", "热狗", "三明治", "鸡肉卷", "可乐鸡翅", "鸡米花", 
              "鸡块", "墨西哥卷饼", "意大利面", "奶昔", "炸鱼薯条", "芝士棒", "烤鸡腿", "烤鸡翅", 
              "鸡肉沙拉", "牛肉汉堡", "双层芝士汉堡", "培根汉堡", "鸡蛋三明治", "火腿三明治", 
              "芝士披萨", "海鲜披萨", "夏威夷披萨", "意式烤肉卷", "炸洋葱圈", "玉米热狗棒", 
              "炸薯饼", "鸡肉饭", "鸡肉意面", "奶油蘑菇汤", "玉米浓汤", "炸虾", "炸鱿鱼圈", 
              "烤玉米", "巧克力奶昔", "草莓奶昔", "香辣鸡翅", "烤鸡胸肉", "鸡肉汉堡", "芝士焗饭"
            ] 
          },
          { 
            name: "日料", 
            foods: [
              "寿司", "刺身", "天妇罗", "拉面", "鳗鱼饭", "章鱼烧", "味增汤", "烤串", "寿喜烧", 
              "牛肉饭", "亲子丼", "乌冬面", "荞麦面", "照烧鸡", "照烧鱼", "烤鳗鱼", "烤三文鱼", 
              "炸猪排", "炸鸡块", "炸虾天妇罗", "日式煎饺", "日式炒面", "日式蛋包饭", "日式咖喱饭", 
              "日式烧肉", "日式烤鱼", "日式豆腐", "章鱼小丸子", "海鲜寿司", "鳗鱼寿司", "三文鱼寿司", 
              "金枪鱼寿司", "蟹肉棒寿司", "黄瓜卷寿司", "加州卷", "天妇罗乌冬", "冷荞麦面", "日式烤鸡串", 
              "日式烤牛肉串", "日式烤猪肉串", "日式煮蛋", "日式茶泡饭", "日式炸鸡", "日式烧鸟", 
              "日式豆皮寿司", "日式章鱼刺身", "日式海胆刺身", "日式鲷鱼烧", "抹茶冰淇淋", "抹茶蛋糕"
            ] 
          }
        ];
        state.selectedGroup = "默认组";
      }
    },
    // 更新某个分组的食物
    updateFoodGroups(state, updatedGroups) {
      state.foodGroups = updatedGroups;
      // 保存到 localStorage
      localStorage.setItem(
        LOCAL_STORAGE_KEY,
        JSON.stringify({
          foodGroups: state.foodGroups,
          selectedGroup: state.selectedGroup,
        })
      );
    },
    // 更新当前选择的组别
    updateSelectedGroup(state, groupName) {
      state.selectedGroup = groupName;
      // 保存到 localStorage
      localStorage.setItem(
        LOCAL_STORAGE_KEY,
        JSON.stringify({
          foodGroups: state.foodGroups,
          selectedGroup: state.selectedGroup,
        })
      );
    },
    // 创建新分组
    createGroup(state, groupName) {
      if (!state.foodGroups.find((group) => group.name === groupName)) {
        state.foodGroups.push({ name: groupName, foods: [] });
        // 保存到 localStorage
        localStorage.setItem(
          LOCAL_STORAGE_KEY,
          JSON.stringify({
            foodGroups: state.foodGroups,
            selectedGroup: state.selectedGroup,
          })
        );
      }
    },
    // 删除分组
    deleteGroup(state, groupName) {
      state.foodGroups = state.foodGroups.filter((group) => group.name !== groupName);
      // 如果删除的组是当前选择的组，重置为默认组
      if (state.selectedGroup === groupName) {
        state.selectedGroup = state.foodGroups.length > 0 ? state.foodGroups[0].name : "";
      }
      // 保存到 localStorage
      localStorage.setItem(
        LOCAL_STORAGE_KEY,
        JSON.stringify({
          foodGroups: state.foodGroups,
          selectedGroup: state.selectedGroup,
        })
      );
    },
        // 添加食物到当前组
    addFoodToGroup(state, { groupName, food }) {
      const group = state.foodGroups.find((g) => g.name === groupName);
      if (group && !group.foods.includes(food)) {
        group.foods.push(food);
        // 保存到 localStorage
        localStorage.setItem(
          LOCAL_STORAGE_KEY,
          JSON.stringify({
            foodGroups: state.foodGroups,
            selectedGroup: state.selectedGroup,
          })
        );
      }
    },
    // 删除食物
    deleteFoodFromGroup(state, { groupName, food }) {
      const group = state.foodGroups.find((g) => g.name === groupName);
      if (group) {
        group.foods = group.foods.filter((f) => f !== food);
        // 保存到 localStorage
        localStorage.setItem(
          LOCAL_STORAGE_KEY,
          JSON.stringify({
            foodGroups: state.foodGroups,
            selectedGroup: state.selectedGroup,
          })
        );
      }
    },
    
  },
  actions: {
  },
  getters: {
    // 获取所有食物
    allFoods(state) {
      return state.foodGroups.flatMap((group) => group.foods);
    },
    // 获取分组
    foodGroups(state) {
      return state.foodGroups;
    },
    // 获取当前选择的组别的食物
    selectedGroupFoods(state) {
      const group = state.foodGroups.find((g) => g.name === state.selectedGroup);
      return group ? group.foods : [];
    },
    // 获取当前选择的组别
    selectedGroup(state) {
      return state.selectedGroup;
    },
    // 获取所选分组的食物
    getFoodBySelectedGroup(state){
      return (groupName)=>{
        const group = state.foodGroups.find((g) => g.name === groupName);
        return group ? group.foods : [];
      }
    }
  },
});
